package com.atguigu.springboot.service.impl;

import com.atguigu.springboot.entity.Department;
import com.atguigu.springboot.mapper.DepartmentMapper;
import com.atguigu.springboot.service.IDepartmentService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

/**
 * @author : wenchao.long
 * @date : Created in 2020/11/26 14:16 周四
 * @description :
 */
@Service
@AllArgsConstructor
@CacheConfig(cacheNames = "dept")
@Slf4j
public class DepartmentServiceImpl implements IDepartmentService {

    private final DepartmentMapper departmentMapper;

    @Cacheable(key = "#id")
    @Override
    public Department getById(Integer id) {
        return departmentMapper.getById(id);
    }

    @CacheEvict(key = "#id")
    @Override
    public void deleteById(Integer id) {
        System.out.println("删除被调用：" + id);
    }

    @Override
    public void insert(Department department) {
        departmentMapper.insert(department);
    }

    @CachePut(key = "#department.id")
    @Override
    public Department update(Department department) {
        departmentMapper.update(department);
        return department;
    }
}
